package edu.ynu.se.xiecheng.achitectureclass.controller;

import edu.ynu.se.xiecheng.achitectureclass.dto.TemperatureDataDTO;
import edu.ynu.se.xiecheng.achitectureclass.service.TemperatureDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = "土壤温度数据接口")
@RestController
@RequestMapping("/api/soil-temperature-data")
@RequiredArgsConstructor
public class SoilTemperatureDataController {
    private final TemperatureDataService temperatureDataService;

    @ApiOperation("获取指定时间范围内的土壤温度数据")
    @GetMapping
    public ResponseEntity<List<TemperatureDataDTO>> getSoilTemperatureData(
            @RequestParam(required = false) Long start,
            @RequestParam(required = false) Long end) {
        // 如果没有提供时间范围，默认查询最近24小时的数据
        if (start == null) {
            start = System.currentTimeMillis() - 24 * 60 * 60 * 1000;
        }
        if (end == null) {
            end = System.currentTimeMillis();
        }
        Date startDate = new Date(start);
        Date endDate = new Date(end);
        return ResponseEntity.ok(temperatureDataService.getTemperatureData(startDate, endDate));
    }

    @ApiOperation("手动触发土壤温度数据采集")
    @PostMapping("/collect")
    public ResponseEntity<Void> collectSoilTemperatureData() {
        temperatureDataService.collectTemperatureData();
        return ResponseEntity.ok().build();
    }
} 